package com.cn.onetrip.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.cn.onetrip.adapter.InfoGridAdapter;
import com.cn.onetrip.objects.ArticleObj;
import com.cn.onetrip.objects.InfosListObj;
import com.cn.onetrip.objects.TipsType;
import com.cn.onetrip.untility.SyncImageLoader;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.untility.SyncImageLoader.OnImageLoadListener;
import com.cn.onetrip.activity.R;

public class InfoGridActivity extends Activity implements OnItemClickListener {
	private GridView gridView;
	private InfoGridAdapter adapter;
	private Button quit;
	private List<InfosListObj> tipsList = new ArrayList<InfosListObj>();
	private SyncImageLoader syncImageLoader;
	int count = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (SysApplication.infosMapList.size() == 0) {
			finish();
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		setContentView(R.layout.activity_info_grid);
		gridView = (GridView) findViewById(R.id.gridView);
		tipsList = SysApplication.infosMapList.get("tips");
		count = tipsList.size() + 1;
		adapter = new InfoGridAdapter(getApplicationContext());
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(this);
		quit = (Button) findViewById(R.id.quit);
		quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
				overridePendingTransition(R.anim.push_left_translate,
						R.anim.pull_right_translate);
			}
		});
		Button more = (Button) findViewById(R.id.more_info);
		more.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						MoreGridActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.push_right_translate,
						R.anim.pull_left_translate);
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg2 >= count && count % 2 == 1) {
			return;
		}

		// int typeID = ((InfosListObj) adapter.getItem(arg2)).tip_type_cd;
		int typeID = tipsList.get(arg2).tip_type_cd;

		if (typeID == TipsType.travels) {
			Intent intent = new Intent(getApplicationContext(),
					TravelActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.push_right_translate,
					R.anim.pull_left_translate);
			return;
		} else {
			ShowMessage(arg2);
			return;
		}

		// Intent intent = new Intent(InfoGridActivity.this,
		// TravelDetailActivity.class);
		// Bundle bundle = new Bundle();
		// int drawableID = 0;
		// switch (typeID) {
		// case TipsType.tickets:
		// drawableID = getResources().getIdentifier("title_ticket",
		// "drawable", getApplicationContext().getPackageName());
		// break;
		// case TipsType.traffic:
		// drawableID = getResources().getIdentifier("title_traffic",
		// "drawable", getApplicationContext().getPackageName());
		// break;
		// case TipsType.todo:
		// drawableID = getResources().getIdentifier("title_play", "drawable",
		// getApplicationContext().getPackageName());
		// break;
		// case TipsType.consume:
		// drawableID = getResources().getIdentifier("title_consume",
		// "drawable", getApplicationContext().getPackageName());
		// break;
		// case TipsType.arm:
		// drawableID = getResources().getIdentifier("title_arm", "drawable",
		// getApplicationContext().getPackageName());
		// break;
		// case TipsType.feast:
		// drawableID = getResources().getIdentifier("title_feast",
		// "drawable", getApplicationContext().getPackageName());
		// break;
		// case TipsType.way:
		// drawableID = getResources().getIdentifier("title_the_way",
		// "drawable", getApplicationContext().getPackageName());
		// break;
		// case TipsType.alert:
		// drawableID = getResources().getIdentifier("title_alert",
		// "drawable", getApplicationContext().getPackageName());
		// break;
		// case TipsType.tips:
		// drawableID = getResources().getIdentifier("title_tip", "drawable",
		// getApplicationContext().getPackageName());
		// break;
		//
		// default:
		// break;
		// }
		// bundle.putInt("title_image", drawableID);
		// // bundle.putInt("title_image", R.drawable.title_detail);
		// bundle.putBoolean("Left", false);
		// bundle.putString("module", ActivityTag.Info);
		// bundle.putInt("itemId", arg2);
		// bundle.putString("title", tipsList.get(arg2).title);
		// bundle.putString("time", tipsList.get(arg2).time);
		// bundle.putString("source", tipsList.get(arg2).source);
		// intent.putExtras(bundle);
		// startActivity(intent);
		// overridePendingTransition(R.anim.push_right_translate,
		// R.anim.pull_left_translate);
	}

	private void ShowMessage(int position) {
		List<ArticleObj> ArticleList = null;
		Map<String, List<InfosListObj>> notesList = SysApplication.infosMapList;
		List<InfosListObj> infosList = notesList.get("tips");
		InfosListObj infosObj = infosList.get(position);
		ArticleList = infosObj.detailedsInfosList;
		// for (int i = 0; i < ArticleList.size(); i++) {
		// ArticleObj articleObj = ArticleList.get(i);
		// adapter.addBooks(articleObj.title, articleObj.icon,
		// articleObj.description);
		// }

		ImageView imageView = null;
		ImageView closeImageView = null;
		TextView titleView = null;
		TextView ticketView = null;
		final Dialog reNameDialog = new Dialog(InfoGridActivity.this,
				R.style.dialog);
		LayoutInflater mLayoutInflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = mLayoutInflater.inflate(R.layout.info_tab, null);
		imageView = (ImageView) view.findViewById(R.id.PlayImage);
		closeImageView = (ImageView) view.findViewById(R.id.info_title_close);
		titleView = (TextView) view.findViewById(R.id.info_title_text);
		ticketView = (TextView) view.findViewById(R.id.PlayExplain);
		syncLoadImage(imageView, ArticleList.get(0).icon);
		titleView.setText(infosObj.title);
		ticketView.setText(ArticleList.get(0).description);
		reNameDialog.setCancelable(true);
		reNameDialog.setContentView(view);
		closeImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				reNameDialog.dismiss();
			}
		});
		reNameDialog.show();
	}

	private void syncLoadImage(final ImageView imageView, String imageUrl) {
		if (syncImageLoader == null) {
			syncImageLoader = new SyncImageLoader(getApplicationContext());
		}

		syncImageLoader.loadImage(imageUrl, new OnImageLoadListener() {

			@Override
			public void onImageLoad(Drawable drawable) {
				if (drawable != null) {
					imageView.setBackgroundDrawable(drawable);
				} else {
					imageView
							.setBackgroundResource(R.drawable.default_image_big);
				}
			}

			@Override
			public void onImageLoad(Integer t, Drawable drawable) {

			}

			@Override
			public void onError() {

			}

			@Override
			public void onError(Integer t) {

			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_left_translate,
					R.anim.pull_right_translate);
		}
		return super.onKeyDown(keyCode, event);
	}
}
